const Router = require('koa-router')
const Shop = require('../dbs/models/shop')

const router = new Router({ prefix: '/shop' })
// 注册接口
router.post('/signup', async (ctx) => {
  if (!ctx.isAuthenticated()) {
    ctx.body = {
      code: -1,
      msg: '请登录'
    }
  } else {
    const shopData = ctx.request.body
    shopData.rating = 4
    shopData.photos = [shopData.photo]
    const shop = await Shop.find({ name: shopData.name })
    if (shop.length) {
      ctx.body = {
        code: -1,
        msg: '已被注册'
      }
      return
    }
    // 把新用户录入数据库
    const result = await Shop.create(shopData)
    if (result) {
      ctx.body = {
        code: 0,
        msg: '注册成功'
      }
    } else {
      ctx.body = {
        code: -1,
        msg: '注册失败'
      }
    }
  }
})

module.exports = router
